#include "StdAfx.h"
#include "sortfiltertablemodel.h"


SortFilterTableModel::SortFilterTableModel( QObject * parent )
:   QSortFilterProxyModel( parent)
{
    setDynamicSortFilter( true );
    setSortLocaleAware ( true );
}

SortFilterTableModel::~SortFilterTableModel()
{

}

QVariant SortFilterTableModel::userData( const QModelIndex & modelIndex, int role ) const
{
    if (modelIndex.column() != 0)
    {
        QModelIndex modIndex = index( modelIndex.row(), 0 );
        return QSortFilterProxyModel::data( modIndex, role );
    }

    return QSortFilterProxyModel::data( modelIndex, role );
}

bool SortFilterTableModel::filterAcceptsRow( int sourceRow, const QModelIndex & sourceParent ) const
{
    QStandardItemModel * modelBase = dynamic_cast<QStandardItemModel*>( sourceModel() );
    if (modelBase == NULL)
    {
        assert( 0 );
        return QSortFilterProxyModel::filterAcceptsRow( sourceRow, sourceParent );
    }

    // Todo:

    return QSortFilterProxyModel::filterAcceptsRow( sourceRow, sourceParent );
}

bool SortFilterTableModel::lessThan( const QModelIndex & left, const QModelIndex & right ) const
{
    QStandardItemModel * modelBase = dynamic_cast<QStandardItemModel*>( sourceModel() );
    if (modelBase == NULL)
    {
        assert( 0 );
        return QSortFilterProxyModel::lessThan( left, right );
    }

    QStandardItem * leftItem = modelBase->itemFromIndex( left );
    QStandardItem * rightItem = modelBase->itemFromIndex( right );

    bool handled = false;
    bool leftIsGreater = false;

    emit lessThanSignal( * leftItem, * rightItem, leftIsGreater, handled );
    if (handled)
    {
        return leftIsGreater;
    }

    return QSortFilterProxyModel::lessThan( left, right );
}
